package tester;

import java.io.File;
import java.util.List;
import java.util.ArrayList;

/**
 * Mangage all the problems in the data directory.
 */

public class ProblemManager {
  public final File dataDir;
  public final List<Problem> problemList = new ArrayList<Problem>();

  public int getProblemCount() {
    return problemList.size();
  }

  public Problem find(String name) {
    for (Problem problem : problemList) {
      if (problem.name.equals(name.toUpperCase())) return problem;
    }
    return null;
  }

  public ProblemManager(String dataDirName) throws InternalErrorException {
    dataDir = new File(dataDirName);
    if (!dataDir.exists() || !dataDir.isDirectory())
      return;
    File[] fileList = dataDir.listFiles();
    for (File file : fileList) {
      if (file.isDirectory())
        problemList.add(new Problem(file.getAbsoluteFile()));
    }
  }

  public void saveToFiles() {
    for (Problem problem : problemList) {
      problem.saveToConfigFile();
    }
  }
}
